//
//  PreferenceController.m
//  soccer
//
//  Created by 上官千城 on 15/9/10.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "OPreferenceController.h"

@interface OPreferenceController ()

@end

@implementation OPreferenceController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"偏好设置";

    //创建开关
    [self creatSwich];
    
    //创建开关
//    [self creatSwich];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
   

    
}

-(void)creatSwich
{
    //只看咨询
    self.consultSwitch = [[UISwitch alloc]init];
//
    
    self.accept = [[UISwitch alloc]init];
//
    
    self.like = [[UISwitch alloc]init];
//
    
    self.num = [[UILabel alloc]init];
    
//    if ([[Account shareHandle]selectAllPreference].count == 0) {
//        NSLog(@"sss");
//        self.consultSwitch.on = NO;
//        self.accept.on = YES;
//        self.like.on = YES;
//    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        cell.textLabel.text = @"允许他人评论";
        self.accept.frame = CGRectMake(WIDTH - 60, 10, 0, 0);
//        if ([[OAccount shareHandle]selectAllPreference].count == 0) {
//            self.accept.on = YES;
//        }
        [cell addSubview:self.accept];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 1) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        cell.textLabel.text = @"按偏好推送消息";
        self.like.frame = CGRectMake(WIDTH - 60, 10, 0, 0);
//        if ([[OAccount shareHandle]selectAllPreference].count == 0) {
//            self.like.on = YES;
//        }
        [cell addSubview:self.like];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }
    if (indexPath.section == 2 && indexPath.row == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        cell.textLabel.text = @"只显示咨询";
        self.consultSwitch.frame = CGRectMake(WIDTH - 60, 10, 0, 0);
//          self.consultSwitch.on = YES;
        [self.consultSwitch addTarget:self action:@selector(consultSwitchAction:) forControlEvents:UIControlEventValueChanged];
        
        [cell addSubview:self.consultSwitch];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }
    if (indexPath.section == 2 && indexPath.row == 1) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        cell.textLabel.text = @"显示图片数量";
        NSMutableArray *array = [NSMutableArray arrayWithObjects:@"3张",@"6张", nil];
        UISegmentedControl *segmented = [[UISegmentedControl alloc]initWithItems:array];
        segmented.selectedSegmentIndex = self.photoNum;
        [segmented addTarget:self action:@selector(photoNum:) forControlEvents:UIControlEventValueChanged];
        [segmented setTintColor:[UIColor greenColor]];
        segmented.frame = CGRectMake(WIDTH - 80, 5, 70, 30);
        [cell addSubview:segmented];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 2 && indexPath.row == 2 ) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        cell.textLabel.text = @"显示评论数";
        UIButton *sub = [UIButton buttonWithType:UIButtonTypeCustom];
        sub.frame = CGRectMake(WIDTH - 75, 10, 20, 20);
        [sub setTitle:@"-" forState:UIControlStateNormal];
        sub.titleLabel.font = [UIFont systemFontOfSize:30];
        [sub setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [sub addTarget:self action:@selector(subAction) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:sub];
        
        //数量
        self.num.frame = CGRectMake(sub.frame.origin.x + sub.frame.size.width +10, sub.frame.origin.y, 20, 20);
//        if ([[OAccount shareHandle]selectAllPreference].count == 0) {
//            NSLog(@"22");
//            self.commentNum = 3;
//            self.num.text = [NSString stringWithFormat:@"%ld",self.commentNum];
//        }
        
        
        [cell addSubview:self.num];
        
        UIButton *add = [UIButton buttonWithType:UIButtonTypeCustom];
        add.frame = CGRectMake(self.num.frame.origin.x + self.num.frame.size.width , sub.frame.origin.y-1, 20, 20);
        [add setTitle:@"+" forState:UIControlStateNormal];
        [add setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        add.titleLabel.font = [UIFont systemFontOfSize:20];
        [add addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:add];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}

-(void)consultSwitchAction:(UISwitch *)button
{
    
    NSLog(@"%d",button.on);
    if (button.on == NO) {
        
//        button.on = YES;
//        [self.tableView reloadData];
        NSLog(@"只显示咨询 %d",self.consultSwitch.on);
    
    }
    else if (button.on == YES)
    {
//        button.on = NO;
        NSLog(@"显示评论 %d",self.consultSwitch.on);

    }
    [self.tableView reloadData];

  
}

//减少评论数量
-(void)subAction
{
//    if (! (self.commentNum > 6 && self.commentNum < 1)) {
//        self.commentNum = 6;
//        
//    }
    if (self.commentNum > 0 && self.commentNum < 7) {
        NSString *str = [NSString stringWithFormat:@"%ld",--self.commentNum];
        self.num.text = str;
    }
    NSLog(@"%ld",self.commentNum);
}
//增加评论数量
-(void)addAction
{
//    if (! (self.commentNum > 6 && self.commentNum < 1)) {
//        self.commentNum = 1;
//        NSLog(@"3333");
//    }
//    
    if (self.commentNum > -1 && self.commentNum < 6) {
        NSString *str = [NSString stringWithFormat:@"%ld",++self.commentNum];
        self.num.text = str;

    }
    NSLog(@"%ld",self.commentNum);

    
}

//咨询显示图片数量
-(void)photoNum:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex == 0) {
        NSLog(@"3张");
        self.photoNum = 0;
    }
    if (seg.selectedSegmentIndex == 1) {
        NSLog(@"6张");
        self.photoNum = 1;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2 ) {
        if ( self.consultSwitch.on == YES) {
            return 1;
        }
        else if (self.consultSwitch.on == NO) {
            return 3;
        }
    }
    
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

//视图即将消失
-(void)viewDidDisappear:(BOOL)animated
{
    OPreference *per = [[OPreference alloc]init];
    per.commentNum = self.commentNum;
    per.accept = self.accept.on;
    per.like = self.like.on;
    per.photoNum = self.photoNum;
    per.consultSwitch = self.consultSwitch.on;
    
    
    NSLog(@"视图消失");

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //创建偏好设置表
    

    

//    [[OAccount shareHandle]selectAllPreference];
//    OPreference  *per = [[[OAccount shareHandle]selectAllPreference] lastObject];
//    self.photoNum = per.photoNum;
//    self.accept.on = per.accept;
//    self.commentNum = per.commentNum;
//    self.like.on = per.like;
//    self.consultSwitch.on = per.consultSwitch;
//    self.num.text = [NSString stringWithFormat:@"%ld",self.commentNum];
    
}

@end
